package com.ubst.smartad.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Vector;

import com.ubst.smartad.dao.TSALogDO;
import com.ubst.smartad.logger.Debug;

public class TSALogMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;
	
	/** 
     * 생성자
     *   
    */ 
	public TSALogMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSALogMngImpl", log_level);
	}
	
	/**
	 * 로그정보 등록
	 * @param logDO
	 * @throws SQLException
	 */
	public void insertLog(TSALogDO logDO) throws SQLException {
		PreparedStatement pstmt=null;
        StringBuffer queryBuf = null;
        
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_LOG_").append(this.getMonth())
				.append(" ( ")
				.append("  LOG_SERIAL ,LOGTYPE ,JOBTYPE ,DALEVEL ,SYSTEMID ")
				.append(" ,USERID ,REMOTEIP ,ACCOUNTID ,SERVICENAME ,DEAILMSG ")
				.append(" ,NSTATUS ,LOGDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ")
				.append(" ) ");

			debug.debug("insertLog ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, logDO.log_serial);
			pstmt.setString(idx++, logDO.logtype);
			pstmt.setInt(idx++, logDO.jobtype);
			pstmt.setString(idx++, logDO.dalevel);
			pstmt.setString(idx++, logDO.systemid);
			pstmt.setString(idx++, logDO.userid);
			pstmt.setString(idx++, logDO.remoteip);
			pstmt.setString(idx++, logDO.accountid);
			pstmt.setString(idx++, logDO.servicename);
			pstmt.setString(idx++, logDO.deailmsg);
			pstmt.setInt(idx++, logDO.nstatus);
			pstmt.setString(idx++, logDO.logdate);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;
        } catch (SQLException e){
			throw e;
        } finally {
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }
	}
	
	/**
	 * 로그정보 수정
	 * @param logDO
	 * @throws SQLException
	 */
	public void updateLog(TSALogDO logDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_LOG_").append(this.getMonth())
				.append(" SET ")
				.append("  LOG_SERIAL=? ,LOGTYPE=? ,JOBTYPE=? ,DALEVEL=? ,SYSTEMID=? ")
				.append(" ,USERID=? ,REMOTEIP=? ,ACCOUNTID=? ,SERVICENAME=? ,DEAILMSG=? ")
				.append(" ,NSTATUS=? ,LOGDATE=? ")
				.append(" WHERE LOG_SERIAL=? ");

			debug.debug("updateLog ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, logDO.log_serial);
			pstmt.setString(idx++, logDO.logtype);
			pstmt.setInt(idx++, logDO.jobtype);
			pstmt.setString(idx++, logDO.dalevel);
			pstmt.setString(idx++, logDO.systemid);
			pstmt.setString(idx++, logDO.userid);
			pstmt.setString(idx++, logDO.remoteip);
			pstmt.setString(idx++, logDO.accountid);
			pstmt.setString(idx++, logDO.servicename);
			pstmt.setString(idx++, logDO.deailmsg);
			pstmt.setInt(idx++, logDO.nstatus);
			pstmt.setString(idx++, logDO.logdate);
			pstmt.setInt(idx++, logDO.log_serial);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	/**
	 * 로그정보 목록 조회
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public TSALogDO selectLog(int log_serial) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSALogDO logDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  LOG_SERIAL ,LOGTYPE ,JOBTYPE ,DALEVEL ,SYSTEMID ")
				.append(" ,USERID ,REMOTEIP ,ACCOUNTID ,SERVICENAME ,DEAILMSG ")
				.append(" ,NSTATUS ,LOGDATE ")
				.append(" FROM TSA_LOG_").append(this.getMonth())
				.append(" WHERE LOG_SERIAL=? ");

			debug.debug("selectLogList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, log_serial);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				logDO = new TSALogDO();
				logDO.log_serial = log_serial;
				logDO.logtype = rs.getString("LOGTYPE");
				logDO.jobtype = rs.getInt("JOBTYPE");
				logDO.dalevel = rs.getString("DALEVEL");
				logDO.systemid = rs.getString("SYSTEMID");
				logDO.userid = rs.getString("USERID");
				logDO.remoteip = rs.getString("REMOTEIP");
				logDO.accountid = rs.getString("ACCOUNTID");
				logDO.servicename = rs.getString("SERVICENAME");
				logDO.deailmsg = rs.getString("DEAILMSG");
				logDO.nstatus = rs.getInt("NSTATUS");
				logDO.logdate = rs.getString("LOGDATE");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return logDO;
	}
	
	/**
	 * 로그정보 삭제
	 * @param userid
	 * @throws SQLException
	 */
	public void deleteLog(int log_serial) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_LOG_").append(this.getMonth())
				.append(" WHERE LOG_SERIAL=? ");

			debug.debug("deleteLog ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, log_serial);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	public String getMonth() {
		Calendar calendar = Calendar.getInstance();
        String month = Integer.toString(calendar.get(Calendar.MONTH)+1);
        if( month.length() == 1 ) {
        	month = "0"+month;
        }
        return month;
	}
}
